package com.ebs.md.api.abs;

/**
 * Revision Author:
 *  $File: //depot/MarketData/commons-md-api/MAIN/src/main/java/com/ebs/md/api/abs/MultipleOrderBookListenerImpl.java $
 *  $Revision: #4 $
 *  $DateTime: 2012/07/12 16:28:40 $
 *  $Change: 112356 $
 *  $Author: Arik.Sher $
 */

import java.util.Calendar;
import java.util.Collections;
import java.util.List;

import com.ebs.md.api.IBookEntry;
import com.ebs.md.api.IOrder;
import com.ebs.md.api.IOrderBook;
import com.ebs.md.api.OrderBookListener;
import com.ebs.md.api.orderbook.BookEntryPosition;
import com.ebs.md.api.orderbook.BookEntryPositionOfDeleted;

/**
 * Holds list of OrderBookListeners and notifies them in same sequence as defined.
 *
 * @param <OB>
 * @param <BE>
 * @param <O>
 */
public class MultipleOrderBookListenerImpl<OB extends IOrderBook<BE>, BE extends IBookEntry, O extends IOrder>
implements OrderBookListener<OB, BE, O>
{
	protected List<OrderBookListener<OB, BE, O>> listeners;
	
	public MultipleOrderBookListenerImpl() {
		super();
		listeners = Collections.emptyList();
	}

	public void setListeners(List<OrderBookListener<OB, BE, O>> listeners) {
		this.listeners = listeners;
	}

	public void init() {
		if ( listeners == null || listeners.isEmpty() ) {
			throw new IllegalArgumentException("missing OrderBook listeners");
		}
	}
	
	@Override
	public void  afterModifyBookEntry(OB orderbook, BE bookEntry, O order) {
		for ( OrderBookListener<OB, BE, O> curr : listeners ) {
			curr.afterModifyBookEntry(orderbook, bookEntry, order);
		}
	}

	@Override
	public void  afterNewBookEntry(OB orderbook, BE newBookEntry, O order, BookEntryPosition position) {
		for ( OrderBookListener<OB, BE, O> curr : listeners ) {
			curr.afterNewBookEntry(orderbook, newBookEntry, order, position);
		}
	}

	@Override
	public void  afterRemoveBookEntry(OB orderbook, BE bookEntry, O order, BookEntryPositionOfDeleted position) {
		for ( OrderBookListener<OB, BE, O> curr : listeners ) {
			curr.afterRemoveBookEntry(orderbook, bookEntry, order, position);
		}
	}

	@Override
	public void  afterSnapshot(OB orderbook, Calendar snapshotTime, boolean isFirst) {
		for ( OrderBookListener<OB, BE, O> curr : listeners ) {
			curr.afterSnapshot(orderbook, snapshotTime, isFirst);
		}
	}

	@Override
	public void  beforeModifyBookEntry(OB orderbook, BE bookEntry, O order) {
		for ( OrderBookListener<OB, BE, O> curr : listeners ) {
			curr.beforeModifyBookEntry(orderbook, bookEntry, order);
		}
	}

	@Override
	public void  beforeNewBookEntry(OB orderbook, BE newBookEntry, O order) {
		for ( OrderBookListener<OB, BE, O> curr : listeners ) {
			curr.beforeNewBookEntry(orderbook, newBookEntry, order);
		}
	}

	@Override
	public void  beforeRemoveBookEntry(OB orderbook, BE bookEntry, O order) {
		for ( OrderBookListener<OB, BE, O> curr : listeners ) {
			curr.beforeRemoveBookEntry(orderbook, bookEntry, order);
		}
	}

	@Override
	public void  beforeSnapshot(OB orderbook, Calendar snapshotTime, boolean isFirst) {
		for ( OrderBookListener<OB, BE, O> curr : listeners ) {
			curr.beforeSnapshot(orderbook, snapshotTime, isFirst);
		}
	}

}
